package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Bed;
import com.karry.yl.domain.baseinfo.Building;
import com.karry.yl.domain.baseinfo.Organization;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.service.baseinfo.BedService;
import com.karry.yl.service.baseinfo.BuildingService;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.baseinfo.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 楼宇信息Controller
 */
@Controller
@RequestMapping("/room")
public class RoomController extends SuperController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private BedService bedService;
    @Autowired
    private OrganizationService organizationService;

    /**
     * 到楼宇列表页
     *
     * @return
     */
    @RequestMapping
    public String listUI(ModelMap map) {
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("buildings", buildings);
        Long orgId = currentUser.getOrgId();
        Organization organization = organizationService.selectById(orgId);
        Integer nature = organization.getNature();
        map.put("userType", nature);
        return "baseinfo/room/list";
    }

    @RequestMapping("/add")
    public String addUI(ModelMap map) {
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("buildings", buildings);
        return "baseinfo/room/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map) {
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("buildings", buildings);
        Room room = roomService.selectById(id);
        map.put("room", room);
        return "baseinfo/room/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit, String buildingId, String floor) {
        long buildingIdNum = 0;
        int floorNum = 0;
        if (!StringUtils.isEmpty(buildingId)) {
            buildingIdNum = Long.parseLong(buildingId);
        }
        if (!StringUtils.isEmpty(floor)) {
            floorNum = Integer.parseInt(floor);
        }
        Page<Room> roomPage = roomService.selectRoomPage(new Page<Room>(page, limit), currentUser.getOrgId(), buildingIdNum, floorNum);
        return new LayTableResponse(roomPage.getRecords(), roomPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Room room) {
        room.setOrgId(currentUser.getOrgId());
        roomService.insert(room);
        return "redirect:/room";
    }

    @RequestMapping("/update")
    public String update(Room room) {
        room.setOrgId(currentUser.getOrgId());
        roomService.updateById(room);
        return "redirect:/room";
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids) {
        roomService.deleteBatchIds(ids);
        bedService.delete(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).in("roomId", ids));
        return "redirect:/room";
    }

    /**
     * 生成房间
     *
     * @param floor
     * @param roomNum
     * @param buildingId
     * @return
     */
    @RequestMapping("/saveList")
    @ResponseBody
    public JsonResponse saveList(Integer[] floor, Integer[] roomNum, Long buildingId) {
        JsonResponse rs;
        Long orgId = currentUser.getOrgId();
        if(floor==null&&roomNum==null){
            return new JsonResponse(0, "无效操作");
        }
        List<Integer> integers = Arrays.asList(roomNum);
        if (Arrays.asList(roomNum).contains(null) ||roomNum.length==0) {
            return new JsonResponse(0, "房间数不能为空");
        }
        if (Arrays.asList(roomNum).contains(0)) {
            return new JsonResponse(0, "房间数不能为0");
        }
        for (Integer s : roomNum) {
            if (s > 300) {
                return rs = new JsonResponse(0, "房间数必须大于0小于300");
            }
        }
        roomService.saveLsit(floor, roomNum, buildingId, orgId);
        return JsonResponse.success();
    }

    /**
     * 查询房号是否重复
     *
     * @param budid
     * @param floor
     * @param code
     * @return
     */
    @RequestMapping("/selectDistinct")
    @ResponseBody
    public JsonResponse selectDistinct(Long budid, Integer floor, String code) {
        Room room = roomService.selectOne(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).eq("buildingId", budid).eq("floor", floor).eq("code", code));
        return JsonResponse.success(room);
    }

    /**
     * 查询某楼的所有楼层
     *
     * @param buildingId
     * @return
     */
    @RequestMapping("/floors")
    @ResponseBody
    public JsonResponse getBuildingId(Long buildingId) {
        Room room = new Room();
        room.setOrgId(currentUser.getOrgId());
        room.setBuildingId(buildingId);
        List<Integer> rooms = roomService.selectRooms(room);
        return JsonResponse.success("rooms", rooms);
    }

    /**
     * 根据楼宇id查房间
     *
     * @param budid
     * @return
     */
    @RequestMapping("/selectRooms")
    @ResponseBody
    public JsonResponse selectRooms(Long budid) {
        Room room = new Room();
        room.setOrgId(currentUser.getOrgId());
        room.setBuildingId(budid);
        List<Integer> rooms = roomService.selectRooms(room);
        return JsonResponse.success(rooms);
    }
}
